package com.example.taobao;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.taobao.data.UserDataDpSQLHelp;
import com.example.taobao.data.userdata;
import com.example.taobao.ui.login.LoginActivity;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class MainActivity extends AppCompatActivity {


    private TextView textView;
    private Button registerBut;
    private EditText userNameEdt,passwordEdt,passwordEdt1,vipNameEdt,iPhoneEdt;
    private Spinner addressSpinner;
    UserDataDpSQLHelp dbHelper = null;
    userdata user_data = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userNameEdt = findViewById(R.id.userid);
        passwordEdt = findViewById(R.id.zhucpassword);
        passwordEdt1 = findViewById(R.id.yespassword);
        vipNameEdt = findViewById(R.id.Vipname);
        iPhoneEdt = findViewById(R.id.iPhone);
        addressSpinner = findViewById(R.id.addressSpinner);






        String[] str = new String[]{"北京", "天津", "上海", "重庆", "新疆", "西藏", "宁夏", "内蒙古",
                "广西", "黑龙江", "吉林", "辽宁", "河北", "山东", "江苏", "安徽",
                "浙江", "福建", "广东", "海南", "云南", "贵州", "四川", "湖南",
                "湖北", "河南", "山西", "陕西", "甘肃", "青海", "江西", "台湾", "香港", "澳门"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,str);

        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

        addressSpinner.setAdapter(adapter);//省份


        registerBut = findViewById(R.id.registerBut);


        registerBut.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                UserDataDpSQLHelp dbHelper = new UserDataDpSQLHelp(getApplicationContext());
                String username = userNameEdt.getText().toString();
                String password = passwordEdt.getText().toString();
                String password1 = passwordEdt1.getText().toString();
                String vipName = vipNameEdt.getText().toString();
                String iphone = iPhoneEdt.getText().toString();
                String address = addressSpinner.getSelectedItem().toString();

                userdata userss = new userdata();
                userss.setUsername(username);
                userss.setPassword(password);
                userss.setVipName(vipName);
                userss.setIphone(iphone);
                userss.setAddress(address);
                userss.setLoginZt(false);
//                user_data = new userdata(username,password,vipName,iphone,address);//创建用户类加入数据

                if (password.equals(password1)) {
                    userss.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                                startActivityForResult(intent, 1);
                                Toast.makeText(MainActivity.this, "注册成功1", Toast.LENGTH_SHORT).show();
                                finish();

                            } else {
                                Toast.makeText(MainActivity.this, "注册失败1", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                }else {
                    Toast.makeText(MainActivity.this, "两次密码不一样", Toast.LENGTH_SHORT).show();
                }




            }

        });



    }

}